package com.yida.gwj.basis.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.basis.service.IGwjBaseSelfBuiltHousesBusinessService;
import com.yida.gwj.common.domain.GwjBaseBuild;
import com.yida.gwj.common.domain.GwjBaseCommunity;
import com.yida.gwj.common.domain.GwjBaseOwner;
import com.yida.gwj.common.domain.dto.GwjBaseHouseDTO;
import com.yida.gwj.common.domain.request.GwjBaseHouseRequest;
import com.yida.gwj.common.domain.request.GwjBaseOwnerRequest;
import com.yida.gwj.common.service.IGwjBaseHouseService;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.IORuntimeException;

/**
 * 自定义导入
 *
 * @author hmh
 * @date 2022-12-15
 */
@RestController
@RequestMapping("/selfBuiltHouses")
public class GwjInitSelfBuiltHousesBusinessController extends BaseController {

    @Autowired
    private IGwjBaseHouseService houseService;

    @Autowired
    private IGwjBaseSelfBuiltHousesBusinessService iSelfBuiltHousesBusinessService;

    /**
     * 查询基础小区列表(根据小区名称)
     */
    @RequiresPermissions("gwj:basis:selfBuiltHouses:list")
    @GetMapping("/communityList")
    public TableDataInfo list(GwjBaseCommunity baseCommunity) {
        if (StringUtils.isNull(baseCommunity.getAreaId())) {
            throw new ServiceException("查无地区，请选择后查询!");
        }
        // 查询导入的项目数据
        baseCommunity.setImportFlag("1");
        startPage();
        List<GwjBaseCommunity> list = iSelfBuiltHousesBusinessService.selectGwjBaseCommunityList(baseCommunity);
        return getDataTable(list);
    }

    /**
     * 查询基础楼栋列表(根据小区ID)
     */
    @RequiresPermissions("gwj:basis:selfBuiltHouses:list")
    @GetMapping("/buildList")
    public TableDataInfo list(GwjBaseBuild baseBuild) {
        startPage();
        List<GwjBaseBuild> list = iSelfBuiltHousesBusinessService.selectGwjBaseBuildList(baseBuild);
        return getDataTable(list);
    }

    /**
     * 查询基础楼栋列表(根据小区ID)
     */
    @RequiresPermissions("gwj:basis:selfBuiltHouses:list")
    @GetMapping("/houseList")
    public TableDataInfo list(GwjBaseHouseRequest houseRequest) {
        startPage();
        List<GwjBaseHouseDTO> list = iSelfBuiltHousesBusinessService.queryGwjBaseHouseListByParam(houseRequest);
        return getDataTable(list);
    }

    /**
     * 查询基础楼栋列表(根据小区ID)
     */
    @RequiresPermissions("gwj:basis:selfBuiltHouses:list")
    @GetMapping("/ownerList")
    public TableDataInfo list(GwjBaseOwnerRequest ownerRequest) {
        startPage();
        List<GwjBaseOwner> list = iSelfBuiltHousesBusinessService.selectGwjBaseOwnerList(ownerRequest);
        return getDataTable(list);
    }

    /**
     * 根据条件查询房屋信息
     * 
     * @param houseRequest 房屋请求数据
     * @return
     */
    @PostMapping("/houseOwnerList")
    @RequiresPermissions("gwj:basis:selfBuiltHouses:list")
    public TableDataInfo queryHouseListByParam(@RequestBody GwjBaseHouseRequest houseRequest) {
        startPage();
        List<GwjBaseHouseDTO> list = houseService.queryGwjBaseHouseListByParam(houseRequest);
        return getDataTable(list);
    }

    /**
     * 小区项目导入
     */
    @Log(title = "自建楼栋-项目导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("gwj:basis:selfBuiltHouses:import")
    @PostMapping("/importDataCommunity")
    public AjaxResult importDataCommunity(MultipartFile file, boolean updateSupport) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("导入项目文件不能为空！");
        }
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
        }
        String message = iSelfBuiltHousesBusinessService.importBaseCommunity(file, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 楼栋数据导入
     */
    @Log(title = "自建楼栋-楼栋导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("gwj:basis:selfBuiltHouses:import")
    @PostMapping("/importDataBuild")
    public AjaxResult importDataBuild(MultipartFile file, @RequestParam(name = "updateSupport") boolean updateSupport, @RequestParam(name = "communityId") Long communityId) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("导入楼栋文件不能为空！");
        }
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
        }
        String message = iSelfBuiltHousesBusinessService.importDataBuild(file, updateSupport, communityId);
        return AjaxResult.success(message);
    }

    /**
     * 房屋数据导入
     */
    // @Log(title = "自建楼栋-房屋导入", businessType = BusinessType.IMPORT)
    // @RequiresPermissions("gwj:basis:selfBuiltHouses:import")
    // @PostMapping("/importDataHouse")
    // public AjaxResult importDataHouse(MultipartFile file, @RequestParam(name = "updateSupport") boolean
    // updateSupport,@RequestParam(name = "communityId") Long communityId) throws Exception {
    // if(!checkImportFile(file)) {
    // return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
    // }
    // //String message = iSelfBuiltHousesBusinessService.importDataHouseAndOwner(file,
    // changeNumToBoolean(updateSupport), communityId);
    // String message = iSelfBuiltHousesBusinessService.importDataHouseAndOwner(file, updateSupport, communityId);
    // return AjaxResult.success(message);
    // }

    /**
     * 20230802修订
     * 
     * @param file 文件类型
     * @param updateSupport 是否更新
     * @param areaId 地区ID
     * @param communityId 项目ID
     * @param buildCode 楼栋标识码
     * @return
     * @throws Exception
     */
    @Log(title = "自建楼栋-房屋导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("gwj:basis:selfBuiltHouses:import")
    @PostMapping("/importDataHouse")
    public AjaxResult importDataHouse(MultipartFile file, @RequestParam(name = "updateSupport") boolean updateSupport, @RequestParam(name = "areaId") Long areaId, @RequestParam(name = "communityId") Long communityId, @RequestParam(name = "buildCode") String buildCode) throws Exception {
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
        }
        // String message = iSelfBuiltHousesBusinessService.importDataHouseAndOwner(file,
        // changeNumToBoolean(updateSupport), communityId);
        String message = iSelfBuiltHousesBusinessService.importDataHouseAndOwner(file, updateSupport, areaId, communityId, buildCode);

        return AjaxResult.success(message);
    }

    /**
     * 批量删除项目
     */
    @Log(title = "自建楼栋-批量删除项目", businessType = BusinessType.DELETE)
    @RequiresPermissions("gwj:basis:selfBuiltHouses:remove")
    @DeleteMapping("/removeCommunitys/{communityIds}")
    public AjaxResult removeCommunitys(@PathVariable Long[] communityIds) {
        return toAjax(iSelfBuiltHousesBusinessService.removeCommunitys(communityIds));
    }

    /**
     * 批量删除楼栋
     */
    @Log(title = "自建楼栋-批量删除楼栋", businessType = BusinessType.DELETE)
    @RequiresPermissions("gwj:basis:selfBuiltHouses:remove")
    @DeleteMapping("/removeBuilds/{buildCodes}")
    public AjaxResult removeBuilds(@PathVariable String[] buildCodes) {
        return toAjax(iSelfBuiltHousesBusinessService.removeBuilds(buildCodes));
    }

    /**
     * 批量删除房屋业主
     */
    @Log(title = "自建楼栋-批量删除房屋业主", businessType = BusinessType.DELETE)
    @RequiresPermissions("gwj:basis:selfBuiltHouses:remove")
    @DeleteMapping("/removeHouses/{houseCodes}")
    public AjaxResult removeHouses(@PathVariable String[] houseCodes) {
        return toAjax(iSelfBuiltHousesBusinessService.removeHouses(houseCodes));
    }

    /**
     * 数值转布尔
     * 
     * @param updateSupport
     * @return
     * 
     */
    private boolean changeNumToBoolean(Integer updateSupport) {
        boolean b = false;
        if (updateSupport == 1) {
            b = true;
        }
        return b;
    }

    /**
     * 检测导入文件类型是否合规
     * 
     * @param file
     * @return
     * @throws IOException
     * @throws IORuntimeException
     */
    private boolean checkImportFile(MultipartFile file) throws Exception {
        // 检查文件类型
        String type = FileTypeUtil.getType(file.getInputStream());
        // 限制的文件类型集合
        if (StringUtils.inStringIgnoreCase(type, "xlsx", "xls")) {
            return true;
        }
        return false;
    }
}
